package acom.gen.testbed.deny;

import java.util.Arrays;
import java.util.Collections;
import java.util.TreeSet;

public class DeniableUtils {
	protected final static String[] oneDomainVerbs = { "Add", "Clone", "CloneAll", "Delete", "DeleteAll", "FindById",
			"FindByName", "ListAll", "ListActive", "SearchByName", "SearchByFields", "SearchByFieldsByPage",
			"SoftDelete", "SoftDeleteAll", "Toggle", "ToggleOne", "Update", "Export", "ExportPDF", "FilterExcel",
			"FilterPDF" };

	public static TreeSet<String> parseVerbDenyString(String domainName, String verbsDenyStr) throws ValidateException {
		TreeSet<String> allverbs = new TreeSet<String>();
		allverbs.addAll(Arrays.asList(oneDomainVerbs));
		TreeSet<String> results = new TreeSet<String>();
		if (verbsDenyStr.equals("none")) {
			return results;
		} else if (verbsDenyStr.equals("max")) {
			results.addAll(allverbs);
			results.remove("ListActive");
			results.remove("SearchByFieldsByPage");
			return results;
		} else {
			String[] tokens = verbsDenyStr.split(",");
			results.addAll(Arrays.asList(tokens));

			ValidateInfo info = new ValidateInfo();
			for (String verbName : results) {
				if (!allverbs.contains(verbName)) {
					info.addCompileError("域对象" + domainName + "中动词名" + verbName + "不正确！");
				}
			}
			for (String verbName : results) {
				if (results.contains("SearchByFieldsByPage")) {
					info.addCompileError("域对象" + domainName + "中动词SearchByFieldsByPage不能否定。");
				}
				if (results.contains("ListActive")) {
					info.addCompileError("域对象" + domainName + "中动词ListActive不能否定。");
				}
				if (results.contains("FindById")) {
					info.addCompileError("域对象" + domainName + "中动词FindById不能否定。");
				}
//				if (results.contains("Add")&&!results.contains("AddAll")){
//					info.addCompileError("域对象"+domainName+"中动词AddAll依赖Add。");
//				}
				if (results.contains("Add") && !results.contains("Clone")) {
					info.addCompileError("域对象" + domainName + "中动词Clone依赖Add。");
				}
				if (results.contains("Add") && !results.contains("CloneAll")) {
					info.addCompileError("域对象" + domainName + "中动词CloneAll依赖Add。");
				}
				if (results.contains("Clone") && !results.contains("CloneAll")) {
					info.addCompileError("域对象" + domainName + "中动词CloneAll依赖Clone。");
				}
				if (results.contains("Delete") && !results.contains("DeleteAll")) {
					info.addCompileError("域对象" + domainName + "中动词DeleteAll依赖Delete。");
				}
				if (results.contains("FindById") && !results.contains("Clone")) {
					info.addCompileError("域对象" + domainName + "中动词Clone依赖FindById。");
				}
				if (results.contains("FindById") && !results.contains("CloneAll")) {
					info.addCompileError("域对象" + domainName + "中动词CloneAll依赖FindById。");
				}
				if (results.contains("ListAll") && !results.contains("Export")) {
					info.addCompileError("域对象" + domainName + "中动词Export依赖ListAll。");
				}
				if (results.contains("ListAll") && !results.contains("ExportPDF")) {
					info.addCompileError("域对象" + domainName + "中动词ExportPDF依赖ListAll。");
				}
				if (results.contains("SearchByFields") && !results.contains("FilterExcel")) {
					info.addCompileError("域对象" + domainName + "中动词FilterExcel依赖SearchByFields。");
				}
				if (results.contains("SearchByFields") && !results.contains("FilterPDF")) {
					info.addCompileError("域对象" + domainName + "中动词FilterPDF依赖SearchByFields。");
				}
				if (results.contains("SoftDelete") && !results.contains("SoftDeleteAll")) {
					info.addCompileError("域对象" + domainName + "中动词SoftDeleteAll依赖SoftDelete。");
				}
				if (results.contains("Toggle") && !results.contains("ToggleOne")) {
					info.addCompileError("域对象" + domainName + "中动词ToggleOne依赖Toggle。");
				}
			}
			if (info.getCompileErrors().size() > 0) {
				throw new ValidateException(info);
			} else {
				return results;
			}
		}
	}
	
	public static TreeSet<String> parseVerbFieldDenyString(String domainName,String verbsDenyStr,String fieldsDemyStr) throws ValidateException{
		TreeSet<String> allverbs = new TreeSet<String>();
		allverbs.addAll(Arrays.asList(oneDomainVerbs));
		
		TreeSet<String> denyedFields = new TreeSet<String>();
		denyedFields.addAll(Arrays.asList(fieldsDemyStr.split(",")));
		TreeSet<String> results = new TreeSet<String>();
		if (denyedFields.contains("domainid")) {
			results.addAll(allverbs);
			results.remove("SearchByFieldsByPage");
		}else {
			results = parseVerbDenyString(domainName,verbsDenyStr);
			if (denyedFields.contains("activefield")) {
				results.add("SoftDelete");
				results.add("SoftDeleteAll");
				results.add("Activate");
				results.add("ActivateAll");
				results.add("Toggle");
				results.add("ToggleOne");
			}else if (denyedFields.contains("activefield")) {
				results.add("FindByName");
				results.add("SearchByName");
			}		
		}	
		return results;
	}

	public static TreeSet<String> parseVerbFieldDenyFieldReadonlyString(String domainName,String verbsDenyStr,String fieldsDemyStr,String fieldsReadonlyStr) throws ValidateException{
		TreeSet<String> results = parseVerbFieldDenyString(domainName,verbsDenyStr,fieldsDemyStr);
		TreeSet<String> readonlyFields = new TreeSet<String>();
		readonlyFields.addAll(Arrays.asList(fieldsReadonlyStr.split(",")));
		if (readonlyFields.contains("domainid")) {
			results.remove("SearchByFieldsByPage");
			results.add("Add");
			results.add("Clone");
			results.add("CloneAll");
			results.add("Delete");
			results.add("DeleteAll");
		}else if (readonlyFields.contains("activefield")) {
			results.add("SoftDelete");
			results.add("SoftDeleteAll");
			results.add("Activate");
			results.add("ActivateAll");
			results.add("Toggle");
			results.add("ToggleOne");
		}
		return results;
	}
}
